<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jsprototype使用</title>
		<script type="text/javascript">
			// 使用构造函数定义一个对象			
			function Student(stuName,stuAge,stuNum){
				// 属性
				this.stuName = stuName;
				this.stuAge = stuAge;
				this.stuNum = stuNum;
				this.stuCardId; // 没有初始化赋值则默认为undefined
				// 行为方法
				this.doStudying = function(){
					console.info(this.stuName +" is studying...");
					console.info("this.stuCardId="+this.stuCardId);
				};
				this.doSleeping = function(){
					console.info(this.stuName + " is sleeping...");
				}
			}
			
			// 使用prototype属性追加一个行为方法
			Student.prototype.doPalying = function(){
				console.info(this.stuName + " is playing...")
			};
			
			Student.prototype.stuSchool = '天津工业大学';
			
			// 使用prototype实现类的继承关系
			function Monitor(){
				
			}
			
			// 实现继承关系
			Monitor.prototype = new Student("小明",20,"15001");
			
			//Monitor.prototype = Student.prototype;
			
			// 创建一个Monitor的实例变量
			var mon0 = new Monitor();
			// 创建一个Student对象
			var stu0 = new Student("小明",20,"15001");
			var stu1 = new Student("小李",21,"15002");
			
			console.dir(stu0);
			console.dir(stu0.__proto__);
			console.info("stu0.__proto__ === Student.prototype："+(stu0.__proto__ === Student.prototype));
			console.info("stu0.__proto__ === stu1.__proto__："+(stu0.__proto__ === stu1.__proto__));
			console.info("stu0.stuAge === stu1.stuAge："+(stu0.stuAge === stu1.stuAge));
			
			console.info("Student.stuAge："+Student.stuAge);
			console.info("stu0.stuAge："+stu0.stuAge);			
			
			// 没有返回值的函数返回值是undefined
			stu0.doPalying();
			
			console.info("stu0.prototype === stu1.prototype："+(stu0.prototype === stu1.prototype));
			console.info("stu0.doSleeping === stu1.doSleeping："+(stu0.doSleeping === stu1.doSleeping));
			console.info("stu0.doPalying === stu1.doPalying："+(stu0.doPalying === stu1.doPalying));
			
			console.info("stu0.stuSchool:"+stu0.stuSchool)
			console.info("stu1.stuSchool:"+stu1.stuSchool)
			console.info("stu0.stuSchool === stu1.stuSchool："+(stu0.stuSchool === stu1.stuSchool));
			
			console.info("mon0.stuSchool:"+mon0.stuSchool)
			mon0.doPalying()
			
			//===================================================
			// 通过prototype给String对象追加自定义的方法
			String.prototype.doSleeping = function(){
				console.info("这是给String类追加的自定义的行为方法...")
			}
			
			var str0 = String();
			str0.doSleeping()
		</script>
	</head>
	<body>
	</body>
</html>
